import { loginUserSsoLoginApi } from '@/api/login';
const useSsoLogin = () => {
	const [loginUrl, setLoginUrl] = useState('');
	// const clientLoginUrl = window.location.origin + '/login';

	const getSsoLoginUrl = () => {
		const loc = window.location.href;
		const clientLoginUrl = loc.replace('sso-login', 'login');
		console.log('[ clientLoginUrl ] >', clientLoginUrl);
		console.log('[ 11111 ] >', `/sso/getSsoAuthUrl?clientLoginUrl=${clientLoginUrl}`);
		loginUserSsoLoginApi({ clientLoginUrl: encodeURIComponent(clientLoginUrl) }).then(res => {
			console.log(res);
			if (res.success) {
				setLoginUrl(res.data as string);
			}
		});
	};

	useEffect(() => {
		getSsoLoginUrl();
	}, []);
	const goLogin = () => {
		console.log('[ loginUrl ] >', loginUrl);

		if (loginUrl) {
			// http://106.14.107.87/ssoServer/sso/auth?client=industrialBrain&redirect=http://localhost:9999/?back=
			// window.location.href = loginUrl.replace('/?back', '/#/login/?/back');
			window.location.href = loginUrl;
		}
	};
	return {
		goLogin,
		loginUrl
	};
};
export default useSsoLogin;
